//
//  CustomCampaignViewController.m
//  MWSDKDemo
//
//  Created by 刘家飞 on 15/5/28.
//  Copyright (c) 2015年 cafei. All rights reserved.
//

#import "CommonCampaignViewController.h"
#import "UIImageView+WebCache.h"
#import "MWApi.h"
#import "Define.h"

@interface CommonCampaignViewController ()

@end

@implementation CommonCampaignViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MWApi pageviewStartWithName:@"commonCampaignView"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //注册活动更新通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadMagicWindowView) name:MWUpdateCampaignNotification object:nil];
    [self loadMagicWindowView];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [MWApi pageviewEndWithName:@"commonCampaignView"];
}

- (void)loadMagicWindowView
{
    //设置加载魔窗位
    BOOL isActivity = [MWApi isActiveOfmwKey:campaign1];
    if (isActivity)
    {
        //banner
        [MWApi configAdViewWithKey:campaign1 withTarget:_bannerImg success:^(NSString *key, UIView *view, MWCampaignConfig *campaignConfig) {
            
            [_bannerImg sd_setImageWithURL:[NSURL URLWithString:campaignConfig.imageUrl] placeholderImage:nil];
            
        } failure:^(NSString *key, UIView *view, NSString *errorMessage) {
            
            //没有获取到相应的活动，该魔窗位自行处理，或隐藏或显示别的内容
            
        }];
    }
    
    if ([MWApi isActiveOfmwKey:campaign2])
    {
        [MWApi configAdViewWithKey:campaign2 withTargetView:_smallImg withTargetViewController:self success:^(NSString * _Nonnull key, UIView * _Nonnull view, MWCampaignConfig * _Nonnull campaignConfig) {
            
            [_smallImg sd_setImageWithURL:[NSURL URLWithString:campaignConfig.imageUrl] placeholderImage:nil];
            
        } failure:^(NSString * _Nonnull key, UIView * _Nonnull view, NSString * _Nullable errorMessage) {
            
            //没有获取到相应的活动，该魔窗位自行处理，或隐藏或显示别的内容
            
        } tap:^BOOL(NSString * _Nonnull key, UIView * _Nonnull view) {
            
            //当用户触发点击事件的时候，可进行某些处理（比如是否登录等）
            return YES;
            
        } mLinkHandler:^NSDictionary * _Nullable(NSString * _Nonnull key, UIView * _Nonnull view) {
            
            //如果这是一个mlink活动，可以返回的该mlink需要的参数
            return nil;
        } mLinkLandingPageHandler:^NSDictionary * _Nullable(NSString * _Nonnull key, UIView * _Nonnull view) {
            //如果这是一个mlink活动，配置landing page ,并且landing page需要传动态参数，可以返回的该mlink landing page 需要的参数
            return nil;
        }];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
